package com.example.administrator.himalaya.criticallies;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.administrator.himalaya.adapter.MyAdapter;
import com.example.administrator.himalaya.R;
import com.example.administrator.himalaya.fragment.FragmentDicsoverAnchor;
import com.example.administrator.himalaya.fragment.FragmentDicsoverCategory;
import com.example.administrator.himalaya.fragment.FragmentDicsoverLive;
import com.example.administrator.himalaya.fragment.FragmentDicsoverRating;
import com.example.administrator.himalaya.fragment.FragmentDicsoverRecommend;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class FirstFragment extends Fragment implements TabLayout.OnTabSelectedListener, View.OnClickListener {
    private TabLayout mTabLayout;
    private ViewPager mPager;
    private List<Fragment> datas;
    private MyAdapter mAdapter;

    public FirstFragment() {

    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View ret = null;
        ret = inflater.inflate(R.layout.fragment_first, container, false);
        mTabLayout = (TabLayout) ret.findViewById(R.id.tab_layout);
        Toolbar toolbar = (Toolbar) ret.findViewById(R.id.bar);
        ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
        ImageView btn = (ImageView) ret.findViewById(R.id.btn);
        btn.setOnClickListener(this);
        mPager = (ViewPager) ret.findViewById(R.id.pager);
        datas = new ArrayList<>();
        datas.add(new FragmentDicsoverRecommend());
        datas.add(new FragmentDicsoverCategory());
        datas.add(new FragmentDicsoverLive());
        datas.add(new FragmentDicsoverRating());
        datas.add(new FragmentDicsoverAnchor());

        mTabLayout.addOnTabSelectedListener(this);
        TabLayout.Tab tab = mTabLayout.newTab();
        tab.setText("推荐");
        mTabLayout.addTab(tab);

        tab = mTabLayout.newTab();
        tab.setText("分类");
        mTabLayout.addTab(tab);

        tab = mTabLayout.newTab();
        tab.setText("广播");
        mTabLayout.addTab(tab);

        tab = mTabLayout.newTab();
        tab.setText("榜单");
        mTabLayout.addTab(tab);

        tab = mTabLayout.newTab();
        tab.setText("主播");
        mTabLayout.addTab(tab);

        mAdapter = new MyAdapter(getChildFragmentManager(),
                getActivity(),
                datas
        );
        mPager.setAdapter(mAdapter);

        mPager.addOnPageChangeListener(
                new TabLayout.TabLayoutOnPageChangeListener(mTabLayout)
        );
        return  ret;
    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {

        mPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }

    @Override
    public void onClick(View view) {
        Toast.makeText(getContext(), "点击了Toobar", Toast.LENGTH_SHORT).show();
    }
}
